<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if (UV::readMQ($route->POST("act")) == 'post')
{
	$user_id = $user->getUserId();
	$title = UV::readMQ(strip_tags($route->POST("title")));
	$preview = UV::readMQ(strip_tags($route->POST("preview"),"<p><h2><h3><blockquote><br/><b><i><sup><strike><ul><li><ol><img><table><tr><td><object><param><embed><a><span>"));
	$text = UV::readMQ(strip_tags($route->POST("full"),"<p><h2><h3><blockquote><br/><b><i><sup><strike><ul><li><ol><img><table><tr><td><object><param><embed><a><span>"));
	$tags = UV::readMQ(strip_tags($route->POST("tags")));
	$mediablock = 0;
	$crop_preview = "";
	$link = "";
	$poll = 0;
	$status = UV::readMQ(strip_tags($route->POST("status")));
	$time = time();
	$order = 0;
	$moder = 0;
	
	$query = "insert into `news` values(NULL,'$user_id','$title','$preview','$text','$tags','$mediablock','$crop_preview','$link','$poll','$status','$time','$order','$moder')";
	if ($db->sendQuery($query))
	{
		$news_id = mysql_insert_id();
		
		// tags
		$tags = explode(",",$tags);
		foreach ($tags as $tag){
			$e_tag = preg_replace('/ +/',' ',trim($tag));
			if ($e_tag != "")
			{
				$is_tag = $db->getCell("select `tag_id` from `tags` where `name` = '$e_tag'");
				if (!$is_tag){
					$db->sendQuery("insert into `tags` values(NULL,'$e_tag')");
					$is_tag = mysql_insert_id();
				}
				$db->sendQuery("insert into `news_tags` values (NULL,'$is_tag','$news_id')");
			}
		}
	}
	else
		$errors[] = "Ошибка добавления новости";
	
	if (count($errors) == 0)
		goToPage("/cabinet/news");
}

require_once CABINET.DS."head.php";
?>
<link href="/files/js/redactor/css/redactor.css" rel="stylesheet" type="text/css" />
<script type='text/javascript' src='/files/js/redactor/redactor.js'></script>
<script type='text/javascript'>
$(document).ready(function(){	
	$('#preview_news').redactor({
		toolbar: 'mini',
		image_upload: "/cabinet/news/upload"
	});
	$('#full_news').redactor({
		image_upload: "/cabinet/news/upload"
	});
	
	$("#show_preview").click(function(){
		$("#preview_title").val($("#title").val());
		$("#preview_preview").val($("#preview_news").val());
		$("#preview_link").val($("#link").val());
		$("#preview_text").val($("#full_news").val());
		$("#preview_tags").val($("#tags").val());
		$("#previewForm").submit();
	});
	
	$("#tag_list").change(function(){
		if ($(this).val() != ''){
			var tag_str = $.trim($("#tags").val());
			var tag_ar = tag_str.split(",");
			for (tag in tag_ar)
				tag_ar[tag] = $.trim(tag_ar[tag]);
			var exists = false;
			for(var i = 0, l = tag_ar.length; i < l; i++){
				if (tag_ar[i] == $(this).val()){
					exists = true;
					break;
				}
			}
			if (!exists){
				if (tag_str != '' && tag_str[tag_str.length-1] != ','){
					tag_str += ",";
				}
				tag_str += $(this).val();
				$("#tags").val(tag_str);
			}
		}
	})
	
	$("#mediaOn").click(function(){
		if ($(this).attr('checked') == 'checked')
			$("#mediablock *:not(#mediaOn)").removeAttr('disabled');
		else
			$("#mediablock *:not(#mediaOn)").attr('disabled','disabled');
	})
});
</script>
<div class="full">
	<div class="submenu_container">
		<a class="submenu_button all_news" href='/cabinet/news/list'>Список новостей</a>
		<a class="submenu_button add_news submenu_button_active" href='/cabinet/news/add'>Добавить новость</a>
	</div>
	<?php
	if (count($errors) > 0)
		echo "<p style='color:red'>".implode("<br />",$errors)."</p>";
	?>
	<h3>Добавить новость</h3>
	<div id="contentaddnews" style='line-height:27px'>
		<form method='post' enctype="multipart/form-data">
			<input type="hidden" name="act" value='post' />
			<div class="left label clear"><label for='title'>Заголовок</label></div><div class="right"><input type="text" id="title" name="title" /></div>
			<div class="left label clear"><label for='tags'>Теги</label></div><div class="right"><input type="text" id="tags" name="tags" /></div>
			<div class="left label clear"><label for='tag_list'>Список тегов</label></div><div class="left" style="margin: 2px 0 0 18px;">
				<select id='tag_list' style='margin-left:10px;'>
					<option value=''>Выберите тег</option><?php
					$query = "select `T`.`name`
						from `tags` as `T`
						left join `news_tags` as `NT` using (`tag_id`)
						group by `T`.`tag_id` having count(`NT`.`tag_id`) > 0 order by count(`NT`.`tag_id`) desc,`name`";
					$tag_list = $db->getTable($query);
					echo selectCreate($tag_list,"name","name");
				?></select>
			</div>
			<div class="clear">
				<label for='order'>Статус</label>
				<input style="margin-left: 60px" type="radio" name="status" id='placed' value='placed' checked='checked' /> <label for='placed'>Размещено</label>
				<input style="margin-left: 10px" type="radio" name="status" id='not' value='not' /> <label for='not'>В черновиках</label>
			</div>
			<div class="left label clear">Краткое содержание</div>
			<div class="clear"><textarea id='preview_news' name='preview'></textarea></div>
			<div class="left label clear">Текст новости</div>
			<div class="clear"><textarea id='full_news' name='full'></textarea></div>
			<div class="right">
				<input type="button" id="show_preview" value="Предварительный просмотр"/> | <input type="submit" value="Опубликовать"/>
			</div>
		</form>
		<form id='previewForm' target='_blank' method='post' action="/cabinet/news/preview">
			<input type='hidden' name='preview_title' id='preview_title' />
			<input type='hidden' name='preview_preview' id='preview_preview' />
			<input type='hidden' name='preview_link' id='preview_link' />
			<input type='hidden' name='preview_text' id='preview_text' />
			<input type='hidden' name='preview_tags' id='preview_tags' />
			<input type='hidden' name='preview_time' id='preview_time' value='<?php echo time(); ?>' />
		</form>
	</div>
</div>

<?php require_once CABINET.DS."footer.php"; ?>
